package com.example.applicationeventdemo.listener;

import com.example.applicationeventdemo.pojo.UserRegisteredEvent;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class UserRegisteredEventListener {

    @EventListener(condition = "#event.username.startsWith('test')")
    //同一个事件多个监听器，@Order指定执行顺序
    @Order(1)
    public void handleUserRegisteredEvent(UserRegisteredEvent event) {
        System.out.println(Thread.currentThread().getName() + "线程Event监听到用户注册事件，用户名: " + event.getUsername());
        // 实际业务：发送邮件、短信等
    }

    @EventListener
    @Order(2)
    public void handleUserRegisteredEvent1(UserRegisteredEvent event) {
        System.out.println(Thread.currentThread().getName() + "线程Event1监听到用户注册事件，用户名: " + event.getUsername());
        // 实际业务：发送邮件、短信等
    }
}